package collect.taobao.util.groupPage;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import collect.taobao.util.*;
import collect.taobao.bean.MailBean;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

//add by www
public class ListGroup extends HibernateDaoSupport {

	//public static int lineNum = 3; // 一页显示几条记录
	public static int maxPage = 0; //总页数    数据库记录数 除 lineNumݿ��¼�� ��
								

	// 获得分页的结果集�Ľ��
	public List getLimitString(int start, String sql,int lineNum) {
		Session session = HibernateUtils.getSession();
		List list = null;
		try {
			// 用sql能多表查询。��sql�ܶ���ѯ��
			list = session.createSQLQuery(
					sql + " limit " + (start - 1) * lineNum + "," + lineNum)
					.list();
		} catch (RuntimeException ex) {
			ex.printStackTrace();
		}
		return list;
	}

	// 获得分页的总页数��
	public Map getMaxPage(String sql,int lineNum) {
		Map map = new HashMap();
		Session session = HibernateUtils.getSession();
		ArrayList<MailBean> arraylist = null;
		int everyPageLine = 0;
		int coutLine = 0;
		try {
			everyPageLine = ((BigInteger) (session.createSQLQuery(
					"select count(*) from (" + sql + ") a").list().get(0)))
					.intValue();
			coutLine = everyPageLine;
		} catch (RuntimeException ex) {
			ex.printStackTrace();
		}
		if (everyPageLine % lineNum == 0) {
			everyPageLine = everyPageLine / lineNum;
		} else {
			everyPageLine = (everyPageLine / lineNum) + 1;
		}
		map.put("everyPageLine", everyPageLine);
		map.put("coutLine", coutLine);
		return map;
	}

}
